﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using System.Windows;
using System.Diagnostics;
using System.Threading;
using System.ComponentModel;

namespace MedicalInterface.ViewModel
{
    public class LoginViewModel : BaseViewModel
    {
        #region variables
        private bool _closeSignal;
        private string _login;
        private string _password;
        private bool _right;
        private UserServiceReference.ServiceUserClient serviceUserClient;
        private ProgressBar _progressbar;
        private UIElement _visibility;
        private BackgroundWorker _worker;
        private Boolean _loginSuccess;
        private UIElement _loginState;
        private String _loginMessage;


        #endregion

        #region commandes
        private ICommand _loginCommand;
        #endregion

        #region getter / setter
        /// <summary>
        /// mot de passe de la personne
        /// </summary>
        public string Password
        {
            get { return _password; }
            set
            {
                if (_password != value)
                {
                    _password = value;
                    OnPropertyChanged("Password");
                }
            }
        }


        /// <summary>
        /// login de la personne
        /// </summary>
        public string Login
        {
            get { return _login; }
            set
            {
                if (_login != value)
                {
                    _login = value;
                    OnPropertyChanged("Login");
                }

            }
        }

        /// <summary>
        /// indique si on doit fermer la fenêtre ou non
        /// </summary>
        public bool CloseSignal
        {
            get { return _closeSignal; }
            set
            {
                if (_closeSignal != value)
                {
                    _closeSignal = value;
                    OnPropertyChanged("CloseSignal");
                }
            }
        }

        /// <summary>
        /// command pour s'authentifier
        /// </summary>
        public ICommand LoginCommand
        {
            get { return _loginCommand; }
            set { _loginCommand = value; }
        }

        /// <summary>
        /// contient la barre de chargement
        /// </summary>
        public ProgressBar ProgressBar
        {
            get { return _progressbar; }
            set { _progressbar = value; }
        }

        /// <summary>
        /// visibilité de la barre de chargement
        /// </summary>
        public UIElement visibility
        {
            get { return _visibility; }
            set { _visibility = value; }
        }

        /// <summary>
        /// statut du login
        /// </summary>
        public UIElement loginState
        {
            get { return _loginState; }
            set { _loginState = value; }
        }

        /// <summary>
        /// Message du login
        /// </summary>
        public String loginMessage
        {
            get { return _loginMessage; }
            set { _loginMessage = value; }
        }

        #endregion

        private bool CanCommandExecute(object parameter)
        {
            // Test On execute quoi qu'il arrive, sera utile pour le remUser
            return true;
        }

        /// <summary>
        /// constructeur
        /// </summary>
        public LoginViewModel()
        {
            //init variables
            base.DisplayName = "Page de login";
            Login = "";
            Password = "";
            visibility = new UIElement();
            loginState = new UIElement();
            visibility.Visibility = Visibility.Hidden;
            loginState.Visibility = Visibility.Visible;
            _loginSuccess = false;
            loginMessage = "";

            serviceUserClient = new UserServiceReference.ServiceUserClient();

            _loginCommand = new RelayCommand(LoginAccess, CanCommandExecute);

            //commandes
            _worker = new BackgroundWorker();
            //on fixe la tache à effectuer
            _worker.DoWork += new DoWorkEventHandler((s, e) =>
            {
                try
                {
                    _loginSuccess = serviceUserClient.Connect(Login, Password);
                    e.Result = "fini";
                }
                catch { }
            });
            //on s'inscrit pour savoir quand la tache est finie
            _worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(_worker_RunWorkerCompleted);

        }

        /// <summary>
        /// évènement lorsque le worker a fini sa tache
        /// </summary>
        void _worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            loginMessage = "try to set error message.";
            if (!_loginSuccess)
            {
                visibility.Visibility = Visibility.Hidden;
                loginState.Visibility = Visibility.Visible;
                loginMessage = "Nom d'utilisateur ou mot de passe incorrect.";
            }
            else
            {
                UserServiceReference.User tmp_u = serviceUserClient.GetUser(Login);
                if (tmp_u.Role == "Infirmière")
                {
                    _right = false;
                }
                else
                    _right = true;
                View.HomeView window = new View.HomeView();
                ViewModel.HomeViewModel vm = new HomeViewModel(_right);
                window.DataContext = vm;
                window.Show();
                CloseSignal = true;
            }
        }

        /// <summary>
        /// action permettant de s'authentifier
        /// </summary>
        private void LoginAccess(object parameter)
        {
            loginState.Visibility = Visibility.Hidden;
            visibility.Visibility = Visibility.Visible;
            _worker.RunWorkerAsync();
        }

    }
}
